package com.ayoka.parenting.ui.group.report;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CircleReportUserCompletedAdapter;
import com.ayoka.parenting.bean.BasicUserInfoBean;
import com.ayoka.parenting.bean.TodayReportBean;
import com.ayoka.parenting.bean.UserInfoBean;
import com.ayoka.parenting.databinding.ActivityTodayReportTableBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.ayoka.parenting.widget.ReportCircleProgressView;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.DateUtil;

import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: TodayReportTableActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class TodayReportTableActivity extends MVPBaseActivity<ActivityTodayReportTableBinding, TodayReportTableContract.View, TodayReportTablePresenter> implements TodayReportTableContract.View {

    public static void start(Activity activity, String topicId, String groupName) {
        Intent intent = new Intent(activity, TodayReportTableActivity.class);
        intent.putExtra("TopicId", topicId);
        intent.putExtra("GroupName", groupName);
        activity.startActivity(intent);
    }

    private String topicId;
    private String groupName;
    private String date;

    private TodayReportBean todayReportBean;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            topicId =intent.getStringExtra("TopicId");
            groupName =intent.getStringExtra("GroupName");
            date = DateUtil.getCurrentDate("yyyyMMdd");
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_today_report_table;
    }


    @Override
    protected void initViews() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.today_report_table);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        if (mPresenter != null) {
            mPresenter.getTodayReportData(topicId, date);
        }
    }

    @Override
    protected TodayReportTablePresenter getPresenter() {
        return new TodayReportTablePresenter();
    }

    @Override
    public void configTodayReportData(TodayReportBean data) {
        if (data == null) return;
        this.todayReportBean = data;
        mDataBinding.tvAllCount.setText(String.valueOf(todayReportBean.MemberCounts));
        mDataBinding.tvCompletedCount.setText(String.valueOf(todayReportBean.TaskCompletedCounts));
        mDataBinding.tvIncompleteCount.setText(String.valueOf(todayReportBean.MemberCounts - todayReportBean.TaskCompletedCounts));

        List<ReportCircleProgressView.CircleReportBean> circleReportBeanList = mPresenter.getCircleReportBeanData(todayReportBean);
        mDataBinding.reportView.setReportBeanData(circleReportBeanList);

        mDataBinding.tvLastTime.setText("数据截止于" + todayReportBean.date);
        mDataBinding.tvCourseCompletedCount.setText(String.valueOf(todayReportBean.CourseCompletedCounts));
        mDataBinding.tvAssignmentCompletedCount.setText(String.valueOf(todayReportBean.AssignmentCompletedCounts));
        mDataBinding.tvMeditationCompletedCount.setText(String.valueOf(todayReportBean.MeditationCompletedCounts));


        if (todayReportBean.CompletedMembers.size() >= todayReportBean.UnCompletedMembers.size()){
            if (todayReportBean.CompletedMembers.isEmpty()) return;
            addCompletedStatusRecyclerView("已完成", todayReportBean.CompletedMembers);
            if (todayReportBean.UnCompletedMembers.isEmpty()) return;
            addCompletedStatusRecyclerView("未完成", todayReportBean.UnCompletedMembers);
        } else {
            if (todayReportBean.UnCompletedMembers.isEmpty()) return;
            addCompletedStatusRecyclerView("未完成", todayReportBean.UnCompletedMembers);
            if (todayReportBean.CompletedMembers.isEmpty()) return;
            addCompletedStatusRecyclerView("已完成", todayReportBean.CompletedMembers);
        }
    }

    private void addCompletedStatusRecyclerView(String title, List<BasicUserInfoBean> members){
        LayoutInflater inflater = LayoutInflater.from(this);
        View containerView = inflater.inflate(R.layout.layout_circle_completed_status_item, null);

        TextView tvTitle = containerView.findViewById(R.id.tv_title);
        tvTitle.setText(title);

        RecyclerView recyclerView = containerView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 7));
        CircleReportUserCompletedAdapter adapter = new CircleReportUserCompletedAdapter(title);
        adapter.setNewData(members);
        adapter.setOnItemClickListener(listener);
        recyclerView.setAdapter(adapter);
        recyclerView.setFocusable(false);

        mDataBinding.llContainer.addView(containerView);
    }

    private BaseQuickAdapter.OnItemClickListener listener = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            UserInfoBean userInfoBean = (UserInfoBean) adapter.getItem(position);
//            UserCourseStatusActivity.start(TodayReportTableActivity.this, date, userInfoBean.ID, userInfoBean.taskCompletedStatus);
        }
    };

}
